package com.travelmood.controller;

import com.travelmood.pojo.Result;
import com.travelmood.pojo.Place;
import com.travelmood.pojo.User;
import com.travelmood.service.PlaceService;
import com.travelmood.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/places")
public class PlaceController {

    @Autowired
    private UserService userService;

    @Autowired
    private PlaceService placeService;

    //新增地点
    @PostMapping("/add")
    public Result addPlace(@RequestHeader("Authorization") String token, @RequestBody Place place) {
        try {
            User user = userService.getUserInfoByToken(token);
            placeService.addPlace(place, user.getUserId());
            return Result.success();
        } catch (Exception e) {
            return Result.error("发生错误，无法添加地点");
        }
    }
    //展示该用户所有地点
    @GetMapping("/myplaces")
    public Result getMyPlaces(@RequestHeader("Authorization") String token) {
        try {
//            if (token.startsWith("Bearer ")) {
//                token = token.substring(7); // 去掉 "Bearer " 前缀
//            }
            User user = userService.getUserInfoByToken(token);
            List<Place> places = placeService.getPlacesByUserId(user.getUserId());
            return Result.success(places);
        } catch (Exception e) {
            log.error("获取地点列表失败", e);  // 打印日志以便调试
            return Result.error("发生错误，无法获取地点列表");
        }
    }
    //展示指定地点
    @GetMapping("/find")
    public Result findPlace(@RequestHeader("Authorization") String token, @RequestParam Integer placeId) {
        try {
            User user = userService.getUserInfoByToken(token);
            Place place = placeService.findPlaceById(placeId);
            if (place == null) {
                return Result.error("未找到地点");
            }
            return Result.success(place);
        } catch (Exception e) {
            log.error("查询地点失败", e);  // 打印日志以便调试
            return Result.error("发生错误，无法查询地点");
        }
    }
    //更新地点内容
    @PutMapping("/update")
    public Result updatePlace(@RequestHeader("Authorization") String token, @RequestBody Place place) {
        try {
            User user = userService.getUserInfoByToken(token);
            placeService.updatePlace(place, user.getUserId());
            return Result.success();
        } catch (Exception e) {
            log.error("更新地点失败", e);  // 打印日志以便调试
            return Result.error("发生错误，无法更新地点");
        }
    }
    //删除
    @DeleteMapping("/delete")
    public Result deletePlace(@RequestHeader("Authorization") String token,@RequestParam Integer placeId) {
        try {
            User user = userService.getUserInfoByToken(token);
            placeService.deletePlaceById(placeId,user.getUserId());
            return Result.success();
        } catch (Exception e) {
            return Result.error("发生错误，无法删除地点");
        }
    }
}
